<template>
    <div class="mainDiv">
        <el-row :gutter="20" style="margin: 0px">
            <el-col :span="12">
                <el-input v-model="subject.name" class="mt mb" placeholder="请输入项目名称">
                    <template slot="prepend">项目名称</template>
                </el-input>

                <el-input v-model="subject.number" placeholder="请输入项目编号">
                    <template slot="prepend">项目编号</template>
                </el-input>

                <el-input v-model="subject.price" class="mt mb" placeholder="请输入项目价格">
                    <template slot="prepend">项目价格</template>
                </el-input>

                <el-input v-model="cname" placeholder="请选择所属分类" disabled>
                    <template slot="prepend">所属分类</template>
                    <el-button slot="append" icon="el-icon-search" @click="selcate"></el-button>
                </el-input>

                <el-input v-model="bname" class="mt mb" placeholder="请选择所属品牌" disabled>
                    <template slot="prepend">所属品牌</template>
                    <el-button slot="append" icon="el-icon-search" @click="selbran"></el-button>
                </el-input>

                <el-input v-model="subject.usetime" placeholder="请输入项目用时默认(min)">
                    <template slot="prepend">项目用时</template>
                </el-input>

                <el-input type="textarea" v-model="subject.remarks" class="mt" placeholder="请输入项目描述"></el-input>

            </el-col>

            <el-col :span="12">

                <!-- 销售提点 -->
                <div class="block mt mb">
                    <span>销售提成类型&nbsp;&nbsp;</span>
                    <el-radio-group v-model="subject.xstdtype">
                        <el-radio :label="1">百分比提成</el-radio>
                        <el-radio :label="2">固定金额提成</el-radio>
                    </el-radio-group>
                </div>

                <el-input v-model="subject.xspercent" placeholder="请输入销售百分比提点（0 ~ 100）" v-if="subject.xstdtype==1">
                    <template slot="prepend">百分比提成</template>
                </el-input>
                <el-input v-model="subject.xsfixed" placeholder="请输入销售固定金额提成" v-else>
                    <template slot="prepend">固定金额提成</template>
                </el-input>
                <!-- 手工提点 -->
                <div class="block mb mt">
                    <span>手工提点类型&nbsp;&nbsp;</span>
                    <el-radio-group v-model="subject.sgtdtype">
                        <el-radio :label="1">百分比提点</el-radio>
                        <el-radio :label="2">固定金额提点</el-radio>
                    </el-radio-group>
                </div>

                <el-input v-model="subject.sgpercent" placeholder="请输入手工百分比提点（0 ~ 100）" v-if="subject.sgtdtype==1">
                    <template slot="prepend">百分比提点</template>
                </el-input>
                <el-input v-model="subject.sgfixed" placeholder="请输入手工固定金额提点" v-else>
                    <template slot="prepend">固定金额提点</template>
                </el-input>
                <!-- 卡耗提成 -->
                <div class="block mb mt">
                    <span>卡耗提点类型&nbsp;&nbsp;</span>
                    <el-radio-group v-model="subject.khtdtype">
                        <el-radio :label="1">百分比提点</el-radio>
                        <el-radio :label="2">固定金额提点</el-radio>
                    </el-radio-group>
                </div>

                <el-input v-model="subject.khpercent" placeholder="请输入卡耗百分比提点（0 ~ 100）" v-if="subject.khtdtype==1">
                    <template slot="prepend">百分比提点</template>
                </el-input>
                <el-input v-model="subject.khfixed" placeholder="请输入卡耗固定金额提点" v-else>
                    <template slot="prepend">固定金额提点</template>
                </el-input>
                <!-- **** -->

            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-button @click.native="close" style="width:90%;display:block;">取消</el-button>
            </el-col>
            <el-col :span="12">
                <el-button type="success" @click.native="callSubmit" style="width:90%;display:block;" :loading="loading">确定</el-button>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import C from '#/src/common/common.js'
export default {
    name: '#list',
    data () {
        return {
            subject: {
                id: 0,
                name: '',
                price: '',
                categoryid: 0,
                brandid: 0,
                usetime: '',
                remarks: '',
                xstdtype: 1,
                xspercent: '',
                xsfixed: '',
                sgtdtype: 1,
                sgpercent: '',
                sgfixed: '',
                khtdtype: 1,
                khpercent: '',
                khfixed: '',
                number: '',
            },
            cname: '',
            bname: '',
        }
    },
    mounted () {
        let RequestUrl = C.getDatas()
        if (RequestUrl.row) {
            let obj = RequestUrl.row
            this.subject.id = obj.id
            this.subject.name = obj.name
            this.subject.price = obj.price
            this.subject.categoryid = obj.categoryid
            this.subject.brandid = obj.brandid
            this.subject.usetime = obj.usetime
            this.subject.remarks = obj.remarks

            this.subject.xstdtype = obj.xstdtype
            this.subject.xspercent = Number(obj.xspercent * 100).toFixed(0)
            this.subject.xsfixed = obj.xsfixed
            this.subject.sgtdtype = obj.sgtdtype
            this.subject.sgpercent = Number(obj.sgpercent * 100).toFixed(0)
            this.subject.sgfixed = obj.sgfixed
            this.subject.khtdtype = obj.khtdtype
            this.subject.khpercent = Number(obj.khpercent * 100).toFixed(0)
            this.subject.khfixed = obj.khfixed

            this.subject.number = obj.number
            this.cname = obj.cname
            this.bname = obj.bname
        }
    },
    methods: {
        selcate: async function () {
            let res = await C.openBrowserWindow('/selectPopupCategory', {}, {
                transparent: true,
                frame: false,
                width: 1000,
                height: 880,
                useSpa: true,
                spaModel: 'organization'
            })
            if (res) {
                this.subject.categoryid = res.id
                this.cname = res.name
            }
        },
        selbran: async function () {
            let res = await C.openBrowserWindow('/selectPopupBrand', {}, {
                transparent: true,
                frame: false,
                width: 1000,
                height: 880,
                useSpa: true,
                spaModel: 'organization'
            })
            if (res) {
                this.subject.brandid = res.id
                this.bname = res.name
            }
        },
        async callSubmit (index) {
            if (/^\s*$/.test(this.subject.name)) {
                C.error('请输入项目名称')
                return false
            } else if (this.subject.number === '') {
                C.error('请输入项目编号')
                return false
            } else if (!/^[1-9]+\d*(\.)?\d*$/.test(this.subject.price * 1)) {
                C.error('请输入项目价格')
                return false
            } else if (this.subject.categoryid == 0) {
                C.error('请选择所属分类')
                return false
            } else if (!/^[1-9]+\d*$/.test(this.subject.usetime * 1)) {
                C.error('请设置项目所需要的时间')
                return false
            }


            if (this.subject.xstdtype == 1) {
                if (!/^(\d|[1-9]\d|100)$/.test(this.subject.xspercent)) {
                    C.error('请设置项目销售时的提成百分比（0 ~ 100）')
                    return false
                }
            } else {
                if (!/^([1-9]\d*|0)(\.\d+)?$/.test(this.subject.xsfixed)) {
                    C.error('请设置项目销售时的提成金额')
                    return false
                }
            }

            if (this.subject.sgtdtype == 1) {
                if (!/^(\d|[1-9]\d|100)$/.test(this.subject.sgpercent)) {
                    C.error('请设置项目手工的提成百分比（0 ~ 100）')
                    return false
                }
            } else {
                if (!/^([1-9]\d*|0)(\.\d+)?$/.test(this.subject.sgfixed)) {
                    C.error('请设置项目手工的提成金额')
                    return false
                }
            }

            if (this.subject.khtdtype == 1) {
                if (!/^(\d|[1-9]\d|100)$/.test(this.subject.khpercent)) {
                    C.error('请设置项目卡耗百分比提成（0 ~ 100）')
                    return false
                }
            } else {
                if (!/^([1-9]\d*|0)(\.\d+)?$/.test(this.subject.khfixed)) {
                    C.error('请设置项目卡耗固定金额提成')
                    return false
                }
            }
            var xssum = 0
            var sgsum = 0
            var khsum = 0
            if (this.subject.xstdtype === 1) {
                xssum = parseInt(this.subject.price) * parseInt(this.subject.xspercent) / 100
            } else {
                xssum = parseInt(this.subject.xsfixed)
            }

            if (this.subject.sgtdtype === 1) {
                sgsum = parseInt(this.subject.price) * parseInt(this.subject.sgpercent) / 100
            } else {
                sgsum = parseInt(this.subject.sgfixed)
            }

            if (this.subject.khtdtype === 1) {
                khsum = parseInt(this.subject.price) * parseInt(this.subject.khpercent) / 100
            } else {
                khsum = parseInt(this.subject.khfixed)
            }
            var allsum = parseInt(xssum + sgsum + khsum)

            if (this.subject.price < allsum) {
                C.error('输入的提成超出项目价格')
                return false
            }
            let that = this
            let data = await C.ajax('/organization/subject/addedit', this.subject, 'post')
            if (data) {
                if (data.errno === 0) {
                    that.loading = false
                    that.$notify({
                        title: '成功',
                        message: data.data,
                        type: 'success'
                    })
                    C.closeBrowserWindow(data)
                } else {
                    that.loading = false
                    that.$notify.error({
                        title: '错误',
                        message: data.errmsg
                    })
                }
            }
        },
        close () {
            C.closeBrowserWindow()
        }
    }
}
</script>

<style>
    .mt {
        margin: 10px 0
    }
    
</style>